package com.fhzn.auth.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fhzn.auth.entity.Resource;
import com.fhzn.auth.entity.Role;
import com.fhzn.auth.entity.User;
import com.fhzn.auth.entity.WebResponse;
import com.fhzn.auth.service.RoleService;
import com.fhzn.auth.tools.PageInfoUtils;
import com.fhzn.auth.web.converter.ResourceConverter;
import com.fhzn.auth.web.converter.RoleConverter;
import com.fhzn.auth.web.converter.UserConverter;
import com.fhzn.auth.web.request.RoleOpRequest;
import com.fhzn.auth.web.request.RoleQueryRequest;
import com.fhzn.auth.web.request.RoleResourceRequest;
import com.fhzn.auth.web.request.RoleUserRequest;
import com.fhzn.auth.web.vo.ResourceVO;
import com.fhzn.auth.web.vo.RoleVO;
import com.fhzn.auth.web.vo.UserVO;
import com.fhzn.commons.toolkit.entity.PageInfo;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;

/**
 * @author 老顾
 * @apiNote 角色管理
 * @since 2023/12/25
 */
@RestController
@RequestMapping("role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @GetMapping("/query")
    public WebResponse<PageInfo<RoleVO>> query(@ParameterObject RoleQueryRequest request) {
        Page<Role> rolePage = roleService.roleWithPage(request);
        return WebResponse.success(PageInfoUtils.convert2page(rolePage, RoleConverter::fromRole));
    }

    @PostMapping("/add")
    public WebResponse<RoleVO> add(@RequestBody RoleOpRequest request) {
        return WebResponse.success(roleService.add(request));
    }

    @PostMapping("/update")
    public WebResponse<String> update(@RequestBody RoleOpRequest request) {
        return WebResponse.success(roleService.update(request));
    }

    @GetMapping("/pageQueryAssignedUsers")
    public WebResponse<PageInfo<UserVO>> pageQueryAssignedUsers(@ParameterObject RoleQueryRequest request) {
        Page<User> userPage = roleService.pageQueryAssignedUsers(request);
        return WebResponse.success(PageInfoUtils.convert2page(userPage, UserConverter::fromUser));
    }

    @PostMapping("/assignUsers")
    public WebResponse<String> assignUsers(@RequestBody RoleUserRequest request) {
        return WebResponse.success(roleService.assignUsers(request));
    }

    @GetMapping("/queryOne")
    public WebResponse<RoleVO> queryOne(@ParameterObject RoleQueryRequest request) {
        return WebResponse.success(roleService.queryOne(request));
    }

    @GetMapping("/pageQueryAssignedResources")
    public WebResponse<PageInfo<ResourceVO>> pageQueryAssignedResources(@ParameterObject RoleQueryRequest request) {
        Page<Resource> resourcePage = roleService.pageQueryAssignedResources(request);
        return WebResponse.success(PageInfoUtils.convert2page(resourcePage, ResourceConverter::fromResource));
    }

    @PostMapping("/configResources")
    public WebResponse<String> configResources(@RequestBody RoleResourceRequest request) {
        return WebResponse.success(roleService.configResources(request));
    }

    @PostMapping("/export")
    public WebResponse<String> export(@RequestBody RoleOpRequest request) {
        return WebResponse.success(roleService.export(request));
    }
}
